<template>
	<view class="content">
		<uni-nav-bar @clickLeft="clickLeft" backgroundColor="#f1f1f1" leftWidth="45rpx" rightWidth="0" leftIcon="left" fixed border="false" statusBar>
			<view class="head-input">
				<u-search v-model="content" focus :clearabled="false" :show-action="false" searchIconColor="#f18b49" bgColor="#ffffff" placeholder="搜索你要购买的商品" class="u-search"></u-search>
				<text v-show="content != ''" class="head-btn" @click="toProDetail()">搜索</text>
			</view>
		</uni-nav-bar>
		<view class="history-record">
			<view class="his-head">
				<view class="head-tit">历史搜索</view>
				<view class="head-del-box">
					<view v-show="showDelAll" class="head-del-tit" @click="deleteAll()">全部清空</view>
					<uni-icons @click="deleteData()" type="trash" size="30" color="#f18b49"></uni-icons>
				</view>
			</view>
			<view class="record-box">
				<view class="tit-box" v-for="item,index in recordList" :key="index">
					<text @click="toProDetail(item.name)">{{ item.name }}</text>
					<image v-show="showDelAll" @click="deleteOne(item.id)" src="../../static/index/delete_search.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: '',
				content: '',
				recordList: [],
				showDelAll: false,
			}
		},
		onLoad(e) {
			this.userId = e.userId
		},
		onShow() {
			this.content = ''
			this.getSearchList()
		},
		methods: {
			// 获取全部搜索记录
			getSearchList() {
				this.recordList = []
				this.showDelAll = false
				this.$getHttp(`/gjcApi/getQueryName?user_id=${this.userId}`, {}, res => {
					res.forEach(v => {
						this.recordList.unshift(v)
					})
				})
			},
			// 删除图标显示 隐藏
			deleteData(){
				if(this.recordList.length == 0){
					uni.showToast({
						title: '搜索记录为空，无需删除',
						duration: 1500,
						icon: 'none'
					})
				} else {
					this.showDelAll =! this.showDelAll
				}
			},
			// 删除全部记录
			deleteAll(){
				uni.showModal({
					cancelText: '取消',
					cancelColor: '#f18b49',
					confirmText: '确定',
					confirmColor: '#999',
					content: '确认清空全部搜索吗？',
					success: (res) => {
						if (res.confirm) {
							this.$getHttp(`/gjcApi/delQueryId?user_id=${this.userId}`, {}, res => {
								if(res.code == 200){
									this.getSearchList()
									uni.showToast({
										title: '全部清空',
										duration: 1000,
										icon: 'none'
									})
								}
							})
						}
					}
				})
			},
			// 单个删除
			deleteOne(id){
				this.$getHttp(`/gjcApi/delQueryId?id=${id}`, {}, res => {
					if(res.code == 200){
						this.getSearchList()
						uni.showToast({
							title: '删除成功',
							duration: 1000,
							icon: 'none'
						})
					}
				})
			},
			// 返回上一页
			clickLeft(){
				uni.navigateBack({
					delta: 1
				})
			},
			//跳转商品列表
			toProDetail(content){
				console.log(content)
				if(content == undefined) {
					uni.navigateTo({
						url: `/pages/class/product-list?userId=${this.userId}&content=${this.content}`
					})
				} else {
					this.content = content
					uni.navigateTo({
						url: `/pages/class/product-list?userId=${this.userId}&content=${content}`
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
page{
	background: $uni-model-color;
}
.content{
	.head-input{
		display: flex;
		align-items: center;
		.u-search{
			width: 648rpx;
			position: relative;
		}
		.head-btn{
			right: 28rpx;
			color: #FFFFFF;
			font-size: 26rpx;
			position: absolute;
			background: #ff914d;
			padding: 10rpx 30rpx;
			border-radius: 40rpx;
		}
	}
	.history-record{
		padding: 30rpx;
		.his-head{
			display: flex;
			align-items: center;
			justify-content: space-between;
			.head-tit{
				font-size: 36rpx;
				font-weight: bold;
			}
			.head-del-box{
				display: flex;
				align-items: center;
				.head-del-tit{
					font-size: 26rpx;
					margin-right: 20rpx;
					padding: 10rpx 20rpx;
					border-radius: 30rpx;
					color: $uni-text-color-grey;
					border: 2rpx solid $uni-text-color-disable;
				}
			}
		}
		.record-box{
			display: flex;
			flex-wrap: wrap;
			padding: 30rpx 0;
			.tit-box{
				display: flex;
				position: relative;
				align-items: center;
				background: #ebebeb;
				border-radius: 15rpx;
				margin: 0 30rpx 30rpx 0;
				text{
					color: #666666;
					font-size: 28rpx;
					padding: 8rpx 20rpx;
				}
				image{
					z-index: 99;
					top: -15rpx;
					right: -14rpx;
					width: 30rpx;
					height: 30rpx;
					position: absolute;
				}
			}
		}
	}
}
</style>
